<?php
class Sosweb_Db_Procedures_FirstPageRanking
{
	private $ratings;
	private $views;
	private $replies;
	private $reports;
	
	public function __construct($ratings = null, $replies = null, $views = null, $reports = null)
	{
		$this->ratings = $ratings;
		$this->views = $views;
		$this->replies = $replies;
		$this->reports = $reports;
	}
	
	public function getRanking()
	{
		//tablica wynikowa najlepszych watkow
		$ranking = array();
		//liczba punktow zapewniajaca pokazanie na 1 stronie stosowana w zgloszeniach przez admina
		$max_points = (4*Sosweb_Const_ProfitmaniaConst::$FIRST_PAGE_DATA_SAMPLE);
		
		//setup points
		$this->ratings = $this->setPoints($this->ratings);
		$this->views = $this->setPoints($this->views);
		$this->replies = $this->setPoints($this->replies);
		
		//zapis danych ratingow do tabeli wynikowej
		foreach ($this->ratings as $rkey=>$rvalue)
		{
			$tmp = new stdClass();
			$tmp->points = $rvalue->points;			
			$tmp->tid = $rvalue->tid;
			$tmp->subject = $rvalue->subject;
			$tmp->category = $rvalue->category;

			array_push($ranking, $tmp);
		}
		
		//aktualizacja listy na podstawie danych o odwiedzinach watku
		foreach ($this->views as $ckey=>$cvalue)
		{
			$is_exist = false;
			
			foreach ($ranking as $ekey=>$evalue)
			{
				if ($cvalue->tid == $evalue->tid)
				{
					$evalue->points = $evalue->points + $cvalue->points;
					$is_exist = true;
					break; 
				}
			}
			
			//nie znaleziono badanego tid wiec dodajemy go do zbioru wynikowego
			if (!$is_exist)
			{
				$tmp = new stdClass();
				$tmp->points = $cvalue->points;
				$tmp->tid = $cvalue->tid;
				$tmp->subject = $cvalue->subject;
				$tmp->category = $cvalue->category;
	
				array_push($ranking, $tmp);								
			}
		}
		
		//aktualizacja listy na podstawie danych o komentarzach watku
		foreach ($this->replies as $ckey=>$cvalue)
		{
			$is_exist = false;
			
			foreach ($ranking as $ekey=>$evalue)
			{
				if ($cvalue->tid == $evalue->tid)
				{
					$evalue->points = $evalue->points + $cvalue->points;
					$is_exist = true;
					break; 
				}
			}
			
			//nie znaleziono badanego tid wiec dodajemy go do zbioru wynikowego
			if (!$is_exist)
			{
				$tmp = new stdClass();
				$tmp->points = $cvalue->points;
				$tmp->tid = $cvalue->tid;
				$tmp->subject = $cvalue->subject;
				$tmp->category = $cvalue->category;
	
				array_push($ranking, $tmp);								
			}
		}		
		
		//aktualizacja listy na podstawie zgloszen administratora
		foreach ($this->reports as $ckey=>$cvalue)
		{
			$is_exist = false;
			
			foreach ($ranking as $ekey=>$evalue)
			{
				if ($cvalue->tid == $evalue->tid)
				{
					$evalue->points = $evalue->points + $max_points;
					$is_exist = true;
					break; 
				}
			}
			
			//nie znaleziono badanego tid wiec dodajemy go do zbioru wynikowego
			if (!$is_exist)
			{
				$tmp = new stdClass();
				$tmp->points = $max_points;
				$tmp->tid = $cvalue->tid;
				$tmp->subject = $cvalue->subject;
				$tmp->category = $cvalue->category;
				
				array_push($ranking, $tmp);								
			}
		}
		
		arsort($ranking);
		
		//ograniczenie listy wynikowej do ilosci okreslonej parametrem
		$loop_counter = Sosweb_Const_ProfitmaniaConst::$FIRST_PAGE_DATA_SAMPLE;
		
		foreach ($ranking as $rkey=>$rvalue)
		{
			if ($loop_counter < 1)
			{
				array_pop($ranking);
			}
			
			$loop_counter--;
		}		
		
//		//wyplucie zawartosci wynikowej tabeli na ekran
//		$loop_counter = 1;
//		
//		foreach ($ranking as $rkey=>$rvalue)
//		{
//			echo '#'.$loop_counter.' TID: '.$rvalue->tid.' TITLE: '.$rvalue->subject.' PTS: '.$rvalue->points.'<br>';
//			$loop_counter++; 
//		}		
		
		return $ranking;
	}
	
	private function setPoints($array)
	{
		$point_counter = Sosweb_Const_ProfitmaniaConst::$FIRST_PAGE_DATA_SAMPLE;
		
		foreach ($array as $akey=>$avalue)
		{
			$avalue->points = ($point_counter < 0 ? 0 : $point_counter);
			$point_counter--;
		}
		
		return $array;
	}
}